package com.ctakit.sdk.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 
 * @author lizhenmin
 *
 */
public class EncoderUtils {

    private static final String PLAIN_SUFFIX = "tdjr!";

    private static final char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9', 'a','b','c','d','e','f'};
    /*
    private static final char[] base64EncodeChars =
            "dzD3yrGSIeKLQjJmBRxOpCoEYhanlTN/50iXbvW64Zqw7Mt1F2UcPkuVgs+A8Hf9$".toCharArray();

    private static final byte[] base64DecodeChars = new byte[]{
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 58, -1, -1, -1, 31,
            33, 47, 49, 3, 40, 32, 39, 44, 60, 63, -1, -1, -1, -1, -1, -1,
            -1, 59, 16, 21, 2, 23, 48, 6, 61, 8, 14, 10, 11, 45, 30, 19,
            52, 12, 17, 7, 29, 50, 55, 38, 35, 24, 41, -1, -1, -1, -1, -1,
            -1, 26, 36, 51, 0, 9, 62, 56, 25, 34, 13, 53, 28, 15, 27, 22,
            20, 42, 5, 57, 46, 54, 37, 43, 18, 4, 1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
    };

    *//**
     * 加入特定后缀后以MD5编码
     * @param text 明文
     * @return 密文
     *//*
    public static String md5Encrypt(String text) {
        return md5(text + PLAIN_SUFFIX);
    }

    *//**
     * 生成MD5编码
     * @param input 明文
     * @return 密文
     */
    public static String md5(String input) {
        return MD5.sign(input);


       /* try {
            // 拿到一个MD5转换器（如果想要SHA1参数换成”SHA1”）
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");

            input = input+PLAIN_SUFFIX;
            // 输入的字符串转换成字节数组
            byte[] inputByteArray = input.getBytes();

            // inputByteArray是输入字符串转换得到的字节数组
            messageDigest.update(inputByteArray);

            // 转换并返回结果，也是字节数组，包含16个元素
            byte[] resultByteArray = messageDigest.digest();

            // new一个字符数组，这个就是用来组成结果字符串的（解释一下：一个byte是八位二进制，也就是2位十六进制字符（2的8次方等于16的2次方））
            char[] resultCharArray =new char[resultByteArray.length * 2];

            // 遍历字节数组，通过位运算（位运算效率高），转换成字符放到字符数组中去
            int index = 0;
            for (byte b : resultByteArray) {
                resultCharArray[index++] = hexDigits[b>>> 4 & 0xf];
                resultCharArray[index++] = hexDigits[b& 0xf];
            }

            // 字符数组组合成字符串返回
            return new String(resultCharArray);
        } catch (NoSuchAlgorithmException e) {
            return null;
        }*/
    }

    /**
     * 基于FNV改进的Hash算法
     * @param input 明文
     * @return Hash值
     *//*
    public static int hashFNV(String input) {
        char[] data = input.toCharArray();
        int hash = (int) 2166136261L;
        for (char c : data) {
            hash = (hash ^ c) * 16777619;
        }
        hash += hash << 13;
        hash ^= hash >> 7;
        hash += hash << 3;
        hash ^= hash >> 17;
        hash += hash << 5;
        return hash;
    }

    *//**
     * 将原始数据编码为base64编码
     * @param input 明文
     * @return 编码后字符串
     *//*
    public static String base64Encode(String input) {
        byte[] data = new byte[0];
        try {
            data = input.getBytes("utf8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        char[] out = new char[((data.length + 2) / 3) * 4];
        for (int i = 0, index = 0; i < data.length; i += 3, index += 4) {
            boolean quad = false;
            boolean trip = false;
            int val = (0xFF & (int) data[i]);
            val <<= 8;
            if ((i + 1) < data.length) {
                val |= (0xFF & (int) data[i + 1]);
                trip = true;
            }
            val <<= 8;
            if ((i + 2) < data.length) {
                val |= (0xFF & (int) data[i + 2]);
                quad = true;
            }
            out[index + 3] = base64EncodeChars[(quad ? (val & 0x3F) : 64)];
            val >>= 6;
            out[index + 2] = base64EncodeChars[(trip ? (val & 0x3F) : 64)];
            val >>= 6;
            out[index + 1] = base64EncodeChars[val & 0x3F];
            val >>= 6;
            out[index] = base64EncodeChars[val & 0x3F];
        }
        return new String(out);
    }

    *//**
     * 将base64编码的数据解码成原始数据
     * @param input 明文
     * @return 编码后字符串
     *//*
    public static String base64Decode(String input) {
        byte[] data = new byte[0];
        try {
            data = input.getBytes("utf8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        int len = ((data.length + 3) / 4) * 3;
        if (data.length > 0 && data[data.length - 1] == '$')
            --len;
        if (data.length > 1 && data[data.length - 2] == '$')
            --len;
        byte[] out = new byte[len];
        int shift = 0;
        int accum = 0;
        int index = 0;
        for (int ix : data) {
            int value = base64DecodeChars[ix & 0xFF];
            if (value >= 0) {
                accum <<= 6;
                shift += 6;
                accum |= value;
                if (shift >= 8) {
                    shift -= 8;
                    out[index++] = (byte) ((accum >> shift) & 0xff);
                }
            }
        }
        if (index != out.length)
            throw new Error("miscalculated data length!");
        return new String(out);
    }

    *//**
     * 将手机号编码
     * @param input 手机号
     * @return 长整数
     *//*
    public static long encodeMobile(String input) {
        input = "00" + input.substring(1);
        short[] data = new short[4];
        byte pos = 0;
        for (int i=0; i<4; i++) {
            data[i] = Short.parseShort(input.substring(pos, pos + 3));
            pos += 3;
        }
        long l1 = (data[0] & -1L) << 30;
        long l2 = (data[1] & 0x3FF) << 20;
        long l3 = (data[2] & 0x3FF) << 10;
        long l4 = data[3] & 0x3FF;
        return l1 | l2 | l3 | l4;
    }

    *//**
     * 将编码过的手机号解码
     * @param input 长整数编码
     * @return 手机号
     *//*
    public static String decodeMobile(long input) {
        String out = "";
        for (int i=0; i<4; i++) {
            long l = (input >> (i * 10)) & 0x3FF;
            out = String.format("%03d", l) + out;
        }
        return "1" + out.substring(2);
    }

*/}
